package fr.gdi.android.news.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import fr.gdi.android.news.FeedDescription;
import fr.gdi.android.news.R;

public class LogUtils
{
    private static final SimpleDateFormat SDF = new SimpleDateFormat("MMM d, yyyy HH:mm:ss ZZZZ");
    
    private LogUtils()
    {
        
    }
    
    private static String getLogs()
    {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = null;
        Process p = null;
        
        try
        {
            p = Runtime.getRuntime().exec(new String[] { "logcat", "-d", "-d", "thread", "-b", "main", "*:D" });
            
            br = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            
            String line = null;
            while ((line = br.readLine()) != null)
            {
                sb.append(line);
                sb.append('\n');
            }
            
            String s = sb.toString();
            return s;
        }
        catch (IOException e)
        {
            return null;
        }
        finally
        {
            if (br != null)
            {
                try
                {
                    br.close();
                }
                catch (IOException e)
                {
                    // swallow
                }
            }
            if (p != null)
            {
                p.destroy();
            }
        }
    }
    
    public static void mail(final Activity ctx)
    {
        AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
        dlg.setTitle(ctx.getString(R.string.send_log_title));
        dlg.setMessage(ctx.getString(R.string.send_log_message));
        dlg.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                collectAndSend(ctx);
            }
        });
        dlg.show();
    }
    
    private static void collectAndSend(final Activity ctx)
    {
        new Thread(new Runnable() {
            public void run()
            {
                StringBuilder content = new StringBuilder();
                
                content
                    .append("Version: ").append(Constants.VERSION).append(IOUtils.NL)
                    .append("Faulty feed index (if relevant -- see below): ").append(IOUtils.NL).append(IOUtils.NL)
                    .append("Max entries (if relevant): ").append(IOUtils.NL).append(IOUtils.NL)
                    .append("Thumbnail size (if relevant -- 0 for none): ").append(IOUtils.NL).append(IOUtils.NL)
                    .append("Problem description: ").append(IOUtils.NL).append(IOUtils.NL)
                    .append("Steps to reproduce: ").append(IOUtils.NL).append(IOUtils.NL);
                 
                content.append(IOUtils.NL);
                
                String logs = getLogs();
                
                List<FeedDescription> feeds = FeedUtils.getFeedDescriptions(ctx, null);
                int k = 0;
                for (FeedDescription fd : feeds)
                {
                    k++;
                    content.append("[").append(k).append("] ").append(fd.name).append(" => ").append(fd.url).append(IOUtils.NL);
                }
                
                content.append(IOUtils.NL).append("----").append(logs);
                
                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType("message/rfc822");
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { Constants.Log.EMAIL_TO });
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, Constants.Log.EMAIL_SUBJECT + SDF.format(new Date()));
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content.toString());
                emailIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                ctx.startActivity(Intent.createChooser(emailIntent, "Send logs"));
            }
        }).start();
    }
    
    //private static createZip
    
}
